package com.qnbc.mystery.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qnbc.mystery.core.controller.BaseController;
import com.qnbc.mystery.core.domain.AjaxResult;
import com.qnbc.mystery.core.page.TableDataInfo;
import com.qnbc.mystery.core.utils.poi.ExcelUtil;
import com.qnbc.mystery.pojo.domain.User;
import com.qnbc.mystery.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qnbc.mystery.pojo.domain.Cart;
import com.qnbc.mystery.service.ICartService;

/**
 * 购物车
 *
 * @author guzj
 * @date 2023-12-27
 */
@Api("购物车")
@RestController
@RequestMapping("/cart")
public class CartController extends BaseController {
    @Autowired
    private ICartService cartService;


    /**
     * 删除购物车
     *
     * @param id 商品id
     * @return result
     */
    @DeleteMapping("/goods/{id}")
    public AjaxResult deleteGoods(@PathVariable Integer id) {

        cartService.deleteCartById(Long.valueOf(id));
        return AjaxResult.success();
    }

    /**
     * 删除购物车
     *
     * @return result
     */
    @DeleteMapping("/empty")
    public AjaxResult empty() {
        User appletCurrentUser = TokenUtils.getCurrentUser();
        LambdaQueryWrapper<Cart> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Cart::getUserId, appletCurrentUser.getOpenId());
        lambdaQueryWrapper.isNull(Cart::getOrderNo);
        cartService.remove(lambdaQueryWrapper);
        return AjaxResult.success();

    }

    /**
     * 查询购物车商品总数量
     */
    @GetMapping("/totalNum")
    public AjaxResult totalNum() {

        Integer totalNum = cartService.getTotalNum();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("totalNum", totalNum);
        return AjaxResult.success(resultMap);
    }

    /**
     * 查询购物车总价格
     */
    @GetMapping("/totalPrice")
    public AjaxResult totalPrice() {
        BigDecimal totalPrice = cartService.getTotalPrice();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("totalPrice", totalPrice);
        return AjaxResult.success(resultMap);
    }

    //todo  运费

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Cart cart) {
        startPage();
        List<Cart> list = cartService.selectCartList(cart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cart cart) {
        List<Cart> list = cartService.selectCartList(cart);
        ExcelUtil<Cart> util = new ExcelUtil<Cart>(Cart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(cartService.selectCartById(id));
    }

    /**
     * 新增购物车
     */
    @ApiOperation(value = "新增购物车")
    @PostMapping
    public AjaxResult add(@RequestBody Cart cart) {
        return toAjax(cartService.insertCart(cart));
    }

    /**
     * 修改购物车
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Cart cart) {
        return toAjax(cartService.updateCart(cart));
    }

    /**
     * 删除购物车
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cartService.deleteCartByIds(ids));
    }
}
